package com.ecloud.support.generator.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ecloud.common.base.result.Result;
import com.ecloud.common.base.result.ResultBuilder;
import com.ecloud.common.base.result.ResultStatus;
import com.ecloud.common.base.result.RowsBuilder;
import com.ecloud.common.base.valid.ValidGroup.Insert;
import com.ecloud.common.base.valid.ValidGroup.QueryList;
import com.ecloud.common.utils.file.FileUtils;
import com.ecloud.support.generator.custom.CustomerConstVal;
import com.ecloud.support.generator.model.dto.TemplateDto;
import com.ecloud.support.generator.model.vo.TemplateVo;
import com.ecloud.support.generator.service.iface.ITemplateService;

/**
 * TemplateController
 *
 * @author LiuGangQiang Create in 2021/04/15
 */
@RestController
@RequestMapping({ "/template" })
public class TemplateController {
    @Resource
    private ITemplateService service;

    /**
     * 查询默认模板功能
     *
     * @author LiuGangQiang Create in 2021/06/09
     * @param dto 入参
     * @return {@link Result}
     * @throws IOException
     */
    @GetMapping(value = { "/default/templates" })
    public Result<?> defaultTemplates(TemplateDto dto) throws IOException {
        /*
         * 获取资源文件 采用Spring提供的PathMatchingResourcePatternResolver 可以支持文件夹以及解决打包后的无法加载相对路径问题
         */
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        org.springframework.core.io.Resource[] resources = resolver.getResources(CustomerConstVal.DEFAULT_TEMPLATE_PATH);
        List<TemplateVo> vos = new ArrayList<>();
        for (org.springframework.core.io.Resource res : resources) {
            TemplateVo vo = new TemplateVo();
            vo.setTemplateName(res.getFilename());
            vo.setContent(FileUtils.read(res.getInputStream()));
            vos.add(vo);
        }
        return ResultBuilder.build(ResultStatus.OK, RowsBuilder.build(vos.size(), vos));
    }

    /**
     * 新增数据
     *
     * @author LiuGangQiang Create in 2021/04/01
     * @param dto 入参
     * @return {@link Result}
     */
    @PostMapping("/templates")
    public Result<?> insertBatch(@RequestBody @Validated({ Insert.class }) List<TemplateDto> dtos) {
        return service.insertBatch(dtos);
    }

    /**
     * 查询列表
     *
     * @author LiuGangQiang Create in 2021/04/01
     * @param dto 入参
     * @return {@link Result}
     */
    @GetMapping("/templates")
    public Result<?> queryList(@Validated({ QueryList.class }) TemplateDto dto) {
        return service.queryList(dto);
    }

}
